package com.app.ctzoverseas.page.main.home

import com.android.base_network.exception.ResponseThrowable
import com.android.base_network.extend.asResponse
import com.android.basemvvm.BaseViewModel
import com.blankj.utilcode.util.LogUtils
import com.cardetective.shopcar.network.model.GoodsDetailInfoModel
import com.cardetective.shopcar.network.model.ShopCarModel
import com.cardetective.shopcar.utils.InjectUtil
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.catch

class ShopCarViewModel : BaseViewModel() {

    private val goodsRepo by lazy { InjectUtil.getGoodsRepo() }

    private val _carList = MutableSharedFlow<ShopCarModel>()
    val carList: SharedFlow<ShopCarModel> = _carList

    private val _checkAll = MutableSharedFlow<String>()
    val checkedAll: MutableSharedFlow<String> = _checkAll

    private val _checkStore = MutableSharedFlow<String>()
    val checkedStore: MutableSharedFlow<String> = _checkStore

    private val _updateCarInfo = MutableSharedFlow<String>()
    val updateCarInfo: MutableSharedFlow<String> = _updateCarInfo

    private val _deleteGoods = MutableSharedFlow<String>()
    val deleteGoods: MutableSharedFlow<String> = _deleteGoods

    private val _checkGoods = MutableSharedFlow<String>()
    val checkGoods: MutableSharedFlow<String> = _checkGoods

    private val _goodsDetail = MutableSharedFlow<GoodsDetailInfoModel>()
    val goodsDetail: SharedFlow<GoodsDetailInfoModel> = _goodsDetail


    // 购物车列表
    fun getCarData() {
        launch {
            goodsRepo.getCarData()
                .asResponse()
                .catch { throwable ->
                    if (throwable is ResponseThrowable) {
                        val responseCode = throwable.getCode()
                        val responseMessage = throwable.getErrMsg()
                        mBVOnExceptionListener?.onListener(responseCode,responseMessage)
                    } else {
                        mBVOnExceptionListener?.onListener(-1,throwable.message.toString())
                    }
                }.collect(_carList)
        }
    }

    // 全选/全不选
    fun checkAllGoodsCar(checked: Boolean) {
        launch {
            goodsRepo.checkAllGoodsCar(checked)
                .asResponse()
                .catch { throwable ->
                    if (throwable is ResponseThrowable) {
                        val responseCode = throwable.getCode()
                        val responseMessage = throwable.getErrMsg()
                        mBVOnExceptionListener?.onListener(responseCode,responseMessage)
                    } else {
                        mBVOnExceptionListener?.onListener(-1,throwable.message.toString())
                    }
                }.collect(_checkAll)
        }
    }

    // 店铺是否选中
    fun checkedStoreCar(checked: Boolean,storeId: String) {
        launch {
            goodsRepo.checkedStoreCar(checked,storeId)
                .asResponse()
                .catch { throwable ->
                    if (throwable is ResponseThrowable) {
                        val responseCode = throwable.getCode()
                        val responseMessage = throwable.getErrMsg()
                        mBVOnExceptionListener?.onListener(responseCode,responseMessage)
                    } else {
                        mBVOnExceptionListener?.onListener(-1,throwable.message.toString())
                    }
                }.collect(_checkStore)
        }
    }

    // 购物车更新商品数量
    fun updateGoodsInCar(num: Int,skuId: String) {
        launch {
            goodsRepo.updateGoodsInCar(num, skuId)
                .asResponse()
                .catch { throwable ->
                    if (throwable is ResponseThrowable) {
                        val responseCode = throwable.getCode()
                        val responseMessage = throwable.getErrMsg()
                        mBVOnExceptionListener?.onListener(responseCode,responseMessage)
                    } else {
                        mBVOnExceptionListener?.onListener(-1,throwable.message.toString())
                    }
                }.collect(_updateCarInfo)
        }
    }

    // 购物车 删除商品
    fun deleteGoodsInCar(skuIds: String) {
        launch {
            goodsRepo.deleteGoodsInCar(skuIds)
                .asResponse()
                .catch { throwable ->
                    if (throwable is ResponseThrowable) {
                        val responseCode = throwable.getCode()
                        val responseMessage = throwable.getErrMsg()
                        mBVOnExceptionListener?.onListener(responseCode,responseMessage)
                    } else {
                        mBVOnExceptionListener?.onListener(-1,throwable.message.toString())
                    }
                }.collect(_deleteGoods)
        }
    }

    // 购物车 选中商品
    fun checkedSingleGoodInCar(skuId: String,checked: Boolean) {
        launch {
            goodsRepo.checkedSingleGoodInCar(skuId, checked)
                .asResponse()
                .catch { throwable ->
                    if (throwable is ResponseThrowable) {
                        val responseCode = throwable.getCode()
                        val responseMessage = throwable.getErrMsg()
                        mBVOnExceptionListener?.onListener(responseCode,responseMessage)
                    } else {
                        mBVOnExceptionListener?.onListener(-1,throwable.message.toString())
                    }
                }.collect(_checkGoods)
        }
    }

    // 切换商品sku
    fun getGoodsBySkuDetail(goodsId: String,goodsSkuId: String) {
        launch {
            goodsRepo.getGoodsBySkuDetail(goodsId, goodsSkuId)
                .asResponse()
                .catch {throwable->
                    if (throwable is ResponseThrowable){
                        val responseCode = throwable.getCode()
                        val responseMessage = throwable.getErrMsg()
                        mBVOnExceptionListener?.onListener(responseCode,responseMessage)
                    }else{
                        mBVOnExceptionListener?.onListener(-1,throwable.message.toString())
                    }
                }.collect(_goodsDetail)
        }
    }


}